package com.yanqu.road.server.timer.fb;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.servercenter.ChannelInfo;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;
import java.util.TreeMap;

/**
 * 计算玩家赚钱速度和银两
 */
public class GetFbFollowCountTask extends GeneralTask {

    private static final Logger log = LogManager.getLogger(GetFbFollowCountTask.class);

    public GetFbFollowCountTask() {

    }

    @Override
    public void doRun() {
        if (ServerListMgr.isOpenFbFollowCount()) {
            ChannelInfo channelConfig = ServerListMgr.getChannelInfoMap().get(ServerListMgr.getServerInfo(ConfigHelper.getLong("serverId")).getChannelId());
            if (!StringUtils.isNullOrEmpty(channelConfig.getFbFollowUrl())) {
                try {
                    Map<String, String> result = HttpHelper.doGet(channelConfig.getFbFollowUrl(), new TreeMap<>());
                    JSONObject jsonObject = JSONObject.parseObject(result.get("body"));
                    int followCount = jsonObject.getInteger("followCount");
                    ServerListMgr.setFbFollowCount(followCount);
                } catch (Exception ex) {
                    log.error(ex);
                }
            }
        }
    }
}
